package k5_net.demo03;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class UDPClient {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        try {
            //创建客户端的socket
            DatagramSocket clientSocket = new DatagramSocket(8888);

            String message = "";
            byte[] byteMessage;

            while(true){
                //要发送的信息
                message = scanner.nextLine();
                //把要发送的数据转成字节数组
                byteMessage = message.getBytes();
                DatagramPacket packet = new DatagramPacket(
                        byteMessage,
                        byteMessage.length,
                        InetAddress.getLocalHost(),
                        9999
                );
                DatagramPacket packet1 = new DatagramPacket(
                        byteMessage,
                        byteMessage.length,
                        InetAddress.getLocalHost(),
                        9998
                );
                clientSocket.send(packet);
                clientSocket.send(packet1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
